package com.athbsd.exer1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    public static void main(String[] args) {
        Collection col = new ArrayList();
        col.add(new Book("三", "罗", "1"));
        col.add(new Book("小", "金", "2"));
        col.add(new Book("红楼梦", "曹雪芹", "3"));
        col.add("jack");
        //System.out.println(col.toString());
        //遍历集合
        //1.先得到col对应的迭代器
        Iterator iterator = col.iterator();
        //2.wihle循环遍历
        while (iterator.hasNext()) {//判断是否还有数据
            Object obj = iterator.next();//返回下一个元素，类型是Object
            System.out.println(obj);
        }//快捷键-->itit
        //3.当推出while循环后，这时iterator迭代器,指向最后的元素。
//        iterator.next();//错误
        System.out.println("------------------------------------");
        //重置迭代器
        iterator = col.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);
        }
    }
}
